package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Slf4j
public class UploadController {
    private final AliyunOSSOperator aliyunOSSOperator;

    public UploadController(AliyunOSSOperator aliyunOSSOperator) {
        this.aliyunOSSOperator = aliyunOSSOperator;
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        log.info("上传参数：file={}", file.getOriginalFilename());
        try {
            String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
            return Result.success(url);
        }catch (Exception e)
        {
            log.error("上传文件失败", e);
            return Result.error("上传文件失败");
        }
    /*public Result upload(String name, Integer age, MultipartFile file) {
        log.info("上传参数：name={},age={},file={}", name, age, file);
        String uuid = UUID.randomUUID().toString();//随机生成文件名
        String filename = file.getOriginalFilename();//获取文件名
        String extName = filename.substring(filename.lastIndexOf("."));//获取文件后缀名
        String newFileName = uuid+extName;
        String path="D:/uoload/";
        File dir = new File(path);
        if (!dir.exists())
        {
            dir.mkdirs();
        }
        try {
            file.transferTo(new File(path, newFileName));
        } catch (Exception e) {
            log.error("上传失败", e);
            return Result.error("上传失败");
        }
        return Result.success();*/
    }
}
